// pages/user/account/withdrawal/withdrawal.js
const app = getApp();
let api = require('../../../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {

    amount: wx.getStorageSync('AMOUNT'),
    badAmount: wx.getStorageSync('BADAMOUNT'),
    badAmountTime: wx.getStorageSync('BADAMOUNTTime'),
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var token = wx.getStorageSync('TOKEN');
    that.getBankCard(token);

    that.setData({
      amount: wx.getStorageSync('AMOUNT'),
      badAmount: wx.getStorageSync('BADAMOUNT'),
      badAmountTime: wx.getStorageSync('BADAMOUNTTime'),
    })
  },
  cardList: function () {
    wx.navigateTo({
      url: '../cardList/cardList',
    })
  },

  /**
   * 获取一张银行卡信息
   */
  getBankCard: function (token) {
    var that = this;
    var backName = wx.getStorageSync('BANKNAME');
    var cardNo = wx.getStorageSync('CARDNO');
    var cardId = wx.getStorageSync('CARDID');
    var card = {
      backName: backName,
      showCard: cardNo.substring(cardNo.length - 4),
      cardId: cardId
    };
    that.setData({
      card: card
    })
  },

  // 输入监听
  inputWacth: function (e) {
    // console.log(e);
    let item = e.currentTarget.dataset.model;
    if (item == 'money') {
      var money;
      if (/^(\d?)+(\.\d{0,2})?$/.test(e.detail.value)) { //正则验证，提现金额小数点后不能大于两位数字
        money = e.detail.value;
      } else {
        money = e.detail.value.substring(0, e.detail.value.length - 1);
      }
      this.setData({
        money: money,
      })
    } else {
      this.setData({
        [item]: e.detail.value
      });
    }
  },
  /**
   * 设置全部 
   */
  setAll: function (e) {
    var that = this;

    this.setData({
      money: that.data.amount,
    })
  },
  /**
   * 提现
   */
  confirmOut: function (e) {
    var that = this;
    var data = that.data;
    if (data.money <= 0 || data.money > data.amount || data.money == undefined) {
      wx.showToast({
        title: '请输入正确的金额',
        icon: 'none'
      })
      return;
    } else {
      var token = wx.getStorageSync('TOKEN');
      var param = {
        "amount": data.money,
        // "cardId": wx.getStorageSync('CARDID')
      }
      // 
      // api._post('/wallet/out', param, token).then(res => {
      //   if (res.code == 200) {
      //     console.log('提交成功');
          
      //   } else {
      //     wx.showToast({
      //       title: '提现错误，请重试',
      //       icon: 'none'
      //     })
      //   }
      // })
      // wx.navigateBack({
      //   delta: 1,
      // })
      wx.showModal({
        title: '提示',
        content: '到账金额会在1-2个工作日',
        success:(res) =>{
          if (res.confirm) {
            api._post('/wallet/out', param, token).then(res => {
              if (res.code == 200) {
                console.log('提交成功');
                
              } else {
                wx.showToast({
                  title: '提现错误，请重试',
                  icon: 'none'
                })
              }
            })
            wx.navigateBack({
              delta: 1,
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  }
})